//
//  BleInfoViewController.m
//  MLBleSDK
//
//  Created by 程杰 on 2023/10/20.
//

#import "BleInfoViewController.h"
#import "MLBleSetViewController.h"
@interface BleInfoViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textViewBroad;
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UILabel *macLb;
@property (weak, nonatomic) IBOutlet UILabel *rssiLb;
@property (weak, nonatomic) IBOutlet UILabel *verLb;



@end

@implementation BleInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.nameLb.text = self.model.name;
    self.rssiLb.text = [NSString stringWithFormat:@"%@",self.model.RSSI];
    
    [SVProgressHUD show];
    [[MLBLEManager sharedManager] setNotifyisSuccess:^(BOOL isSuccess) {
        if (isSuccess) {
            [SVProgressHUD dismiss];
            
            MLBleParamsItem *item = [MLBleParamsItem new];
            item.verType = @"v2.0";
            item.codeType = @"23";// 获取Mac地址
            item.peripheral = self.model.peripheral;
            [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
                self.macLb.text = infos.mac;
                
                
                item.codeType = @"09"; // 获取模块版本号
                [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
                    self.verLb.text = infos.modelVer;
        
                    
                }];
                
            }];
            
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
- (void)viewDidDisappear:(BOOL)animated {
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (self.isMovingFromParentViewController) { // 判断页面是否是通过返回按钮被移除的
        NSLog(@"返回按钮被点击了");
        [[MLBLEManager sharedManager] disconnectFromPeripheral:self.model.peripheral];
    }
    
    
    
}

- (IBAction)searchAction:(id)sender {
    
    MLBleSetViewController *vc = [MLBleSetViewController new];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)chanceAction:(id)sender {
    
    MLBleParamsItem *item = [MLBleParamsItem new];
    item.codeType = @"25";// 开始厂测
    item.peripheral = self.model.peripheral;
    [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
        if(infos.code == 1) {
            [SVProgressHUD showSuccessWithStatus:infos.message];
            
            item.codeType = @"-100"; // 厂测数据结果
            [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
                
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"厂测数据上报" message:infos.message preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"OK button tapped");
                }];

                [alertController addAction:defaultAction];

                [self presentViewController:alertController animated:YES completion:nil];
                
            }];
            
            
        } else {
            [SVProgressHUD showErrorWithStatus:infos.message];
        }
    }];
    
}
- (IBAction)otaAction:(id)sender {
    
    [SVProgressHUD showInfoWithStatus:@"敬请期待！"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
